package com.lzh.template.manager;

import cn.hutool.json.JSONObject;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Encryption {
    public Encryption() {
    }

    public static String SpecificEncryption(String data, String key) {
        SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        String signature = null;

        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signinKey);
            String code = URLEncoder.encode(data, "utf-8");
            code = code.replaceAll("\\+", "%20");
            byte[] result = mac.doFinal(code.getBytes());
            if (null != result) {
                BigInteger bigInteger = new BigInteger(1, result);
                String re16 = String.format("%40s", bigInteger.toString(16));
                re16 = re16.replace(" ", "0");
                signature = re16.toUpperCase();
            }
        } catch (Exception e) {
            e.printStackTrace();
            signature = null;
        }

        return signature;
    }

    public static String DealWithParametersFromURL(Map<String, String> params) {
        StringBuilder result = new StringBuilder();
        List<String> keyList = new ArrayList(params.keySet());

        for (int i = 0; i < keyList.size(); ++i) {
            for (int j = i + 1; j < keyList.size(); ++j) {
                boolean compareFlag = CompareTwoString((String) keyList.get(i), (String) keyList.get(j));
                if (!compareFlag) {
                    String keyS = (String) keyList.get(i);
                    keyList.set(i, keyList.get(j));
                    keyList.set(j, keyS);
                }
            }
        }

        for (String key : keyList) {
            result.append(key);
            result.append((String) params.get(key));
        }

        return result.toString();
    }

    private static boolean CompareTwoString(String str1, String str2) {
        String letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        int str1length = str1.length();
        int str2length = str2.length();
        int index1 = 0;
        int index2 = 0;
        if (str1length <= str2length) {
            for (int i = 0; i < str1length; ++i) {
                index1 = letter.indexOf(str1.substring(i, i + 1));
                index2 = letter.indexOf(str2.substring(i, i + 1));
                if (index1 < index2) {
                    return true;
                }

                if (index1 > index2) {
                    return false;
                }
            }

            return true;
        } else {
            for (int i = 0; i < str2length; ++i) {
                index1 = letter.indexOf(str1.substring(i, i + 1));
                index2 = letter.indexOf(str2.substring(i, i + 1));
                if (index1 < index2) {
                    return true;
                }

                if (index1 > index2) {
                    return false;
                }
            }

            return false;
        }
    }

    public static String DealWithParametersFromURL(JSONObject params) {
        StringBuilder result = new StringBuilder();
        List<String> keyList = new ArrayList(params.keySet());

        for (int i = 0; i < keyList.size(); ++i) {
            for (int j = i + 1; j < keyList.size(); ++j) {
                boolean compareFlag = CompareTwoString((String) keyList.get(i), (String) keyList.get(j));
                if (!compareFlag) {
                    String keyS = (String) keyList.get(i);
                    keyList.set(i, keyList.get(j));
                    keyList.set(j, keyS);
                }
            }
        }

        for (String key : keyList) {
            result.append(key);
            result.append(params.get(key));
        }

        return result.toString();
    }
}
